package com.wise.mall.admin.controller.ums;

import cn.hutool.json.JSONUtil;
import com.wise.mall.admin.controller.BaseController;
import com.wise.mall.admin.dto.request.UmsAdminLoginParam;
import com.wise.mall.admin.dto.request.UmsAdminParam;
import com.wise.mall.admin.dto.response.UmsAdmin;
import com.wise.mall.admin.service.ums.AdminService;
import com.wise.mall.common.constant.AuthConstant;
import com.wise.mall.common.domain.response.UserDto;
import com.wise.mall.common.exception.Asserts;
import com.wise.mall.common.model.Result;
import com.wise.mall.common.model.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;

/**
 * 后台用户管理
 *
 * @author shiweinan
 */
@RestController
@Api(tags = "AdminController", description = "后台用户管理")
@RequestMapping("/admin")
public class AdminController extends BaseController {

//    @Autowired
//    private AdminService adminService;


    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result<UmsAdmin> register(@Validated @RequestBody UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = adminService.register(umsAdminParam);
        return Result.success(umsAdmin);
    }

    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        return adminService.login(umsAdminLoginParam.getUsername(),umsAdminLoginParam.getPassword());
    }

    @GetMapping("/loadByUsername")
    public Result<UserDto> loadUserByUsername(String username){
        return Result.success(adminService.loadUserByUsername(username));
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public Result getAdminInfo() {
//        UmsAdmin currentUser = getCurrentUser();
        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
        if (StringUtils.isEmpty(userStr)) {
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        UmsAdmin umsAdmin = JSONUtil.toBean(userStr, UmsAdmin.class);
        umsAdmin = adminService.loadAdminUserById(umsAdmin.getId());
//        UmsAdmin umsAdmin = adminService.getCurrentAdmin();
//        Map<String, Object> data = new HashMap<>();
//        data.put("username", umsAdmin.getUsername());
//        data.put("menus", roleService.getMenuList(umsAdmin.getId()));
//        data.put("icon", umsAdmin.getIcon());
//        List<UmsRole> roleList = adminService.getRoleList(umsAdmin.getId());
//        if(CollUtil.isNotEmpty(roleList)){
//            List<String> roles = roleList.stream().map(UmsRole::getName).collect(Collectors.toList());
//            data.put("roles",roles);
//        }
//        return Result.success(data);
        return Result.success(umsAdmin);
    }

    @GetMapping("/test")
    public Result test(){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("test1",LocalDateTime.now());
        map.put("test2",new Date());
        return Result.success(map);
    }



}
